package com.lile.stringBuffer;

public class StringReverse {

	public static void main(String[] args) {
		String s = "DCBA";

		System.out.println(stringReverse1(s));
		System.out.println(stringReverse2(s));
		System.out.println(stringReverse3(s));
	}

	public static String stringReverse1(String str) {
		String s = "";

		char[] chs = str.toCharArray();
		for (int i = chs.length - 1; i >= 0; i--) {
			s += chs[i];
		}

		return s;
	}

	public static String stringReverse2(String str) {
		// 把字符串转成数组
		char[] chs = str.toCharArray();
		// 把数组的元素反转
		for (int start = 0, end = chs.length - 1; start < end; start++, end--) {
			char temp = chs[start];
			chs[start] = chs[end];
			chs[end] = temp;
		}
		// 把数组转成字符串返回
		return String.valueOf(chs);
	}

	public static String stringReverse3(String str) {
		StringBuffer sb = new StringBuffer(str);
		sb.reverse();
		return new String(sb);
	}

}
